﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Text.RegularExpressions;
using SipII.Milnets;
using SipII.Message;
using SipII.Parameter;

namespace SipII
{
    partial class Sip2Server4Milnets : ISip2Server
    {
        const string Error_TerminalPasswordNoMatch = "终端密码未通过验证";
        const string Error_SystemNotSupported = "系统目前尚不支持此操作";
        const string Error_AcsNotSupported = "图书馆自动化系统不支持此操作";
        const string Error_PatronNotExist = "数据库中未找到条码为 {0} 的读者";
        const string Error_ItemNotExist = "数据库中未找到条码为 {0} 的文献";
        const string Error_ItemNotCharged = "条码为 {0} 的读者并未借阅条码为 {1} 的文献";
        const string Error_ItemHasCherged = "条码为 {0} 的文献已经被条码为 {0} 的读者借出";
        const string Error_ItemCheckoutDenied4Status = "文献的状态标识声明该文献禁止借阅";
        const string Error_ItemStatusIsNotCharged = "条码为 {0} 的文献并未被借出";
        const string Error_ItemDuplicateRenewed = "读者已经对文献执行过续借操作";
        const string Error_PatronRenewDenied = "读者续借文献被拒，可能是由于系统不支持或读者无权限";
        const string Error_CheckoutDenied4Status = "读者由于证件非正常状态或政策限制，借书操作被拒";
        const string Error_CheckoutDenied4CardLost = "读者由于证件被挂失，借书操作被拒";
        const string Error_CheckoutDenied4ChargedLimit = "读者由于在借图书过多，借书操作被拒";
        const string Error_CheckoutDenied4OverdueLimit = "读者由于超期图书过多，借书操作被拒";
        const string Error_CheckoutDenied4FeeLimit = "读者由于欠费过多，借书操作被拒";
        const string Error_CheckoutDenied4PolicyUnavailable = "因为无法获取借阅政策，借书操作被拒";
        const string Error_CheckoutDenied4BibinfoUnavailable = "因为无法获取编目信息，借书操作被拒";
        const string Error_CheckoutDenied4ClassChargedLimit = "因为{0}类图书限借{1}本，借书操作被拒";
        const string Error_RenewPolicyUnknow = "续借政策不明";
        const string Info_Renewall = "{0}本续借成功，{1}续借失败";
        const string Info_CheckinBySc = "自助归还";

        /// <summary>
        /// 获取系统支持信息
        /// </summary>
        /// <remarks >ACS系统支持的消息均在app.config文件中配置</remarks>
        /// <returns></returns>
        private char[] getSupportedMessage()
        {
            char[] SupportedMessages = new char[16];

            SupportedMessages[0] = Properties.Settings.Default.PatronStatusRequest_Enable ? 'Y' : 'N';  ///支持查询读者状态
            SupportedMessages[1] = Properties.Settings.Default.Checkout_Enable ? 'Y' : 'N';             ///支持借书
            SupportedMessages[2] = Properties.Settings.Default.Checkin_Enable ? 'Y' : 'N';              ///支持还书
            SupportedMessages[3] = Properties.Settings.Default.BlockPatron_Enable ? 'Y' : 'N';          ///支持停用借书证
            SupportedMessages[4] = Properties.Settings.Default.Sc_Acs_Status_Enable ? 'Y' : 'N';        ///支持通报SC/ACS状态
            SupportedMessages[5] = Properties.Settings.Default.Resend_Enable ? 'Y' : 'N';               ///支持请求SC/ACS重发
            SupportedMessages[6] = Properties.Settings.Default.Login_Enable ? 'Y' : 'N';                ///支持响应登录
            SupportedMessages[7] = Properties.Settings.Default.PatronInformation_Enable ? 'Y' : 'N';    ///支持查询读者信息
            SupportedMessages[8] = Properties.Settings.Default.EndPatronSession_Enable ? 'Y' : 'N';     ///支持结束读者会话
            SupportedMessages[9] = Properties.Settings.Default.FeePaid_Enable ? 'Y' : 'N';              ///支持缴费
            SupportedMessages[10] = Properties.Settings.Default.ItemInformation_Enable ? 'Y' : 'N';     ///支持查询图书信息
            SupportedMessages[11] = Properties.Settings.Default.ItemStatusUpdate_Enable ? 'Y' : 'N';    ///支持更改图书状态
            SupportedMessages[12] = Properties.Settings.Default.PatronEnable_Enable ? 'Y' : 'N';        ///支持启用借书证
            SupportedMessages[13] = Properties.Settings.Default.Hold_Enable ? 'Y' : 'N';                ///支持预约
            SupportedMessages[14] = Properties.Settings.Default.Renew_Enable ? 'Y' : 'N';               ///支持续借
            SupportedMessages[15] = Properties.Settings.Default.Renew_Enable ? 'Y' : 'N';               ///支持批量续借

            return SupportedMessages;
        }

        /// <summary>
        /// 检查SC设备登录是否成功
        /// </summary>
        /// <param name="sessionId"></param>
        /// <param name="request"></param>
        /// <param name="terminalLocation"></param>
        /// <remarks >终端设备的登录信息记录在ScTerminalInfo.xml文件中，登录时要做到locationCode、loginUserId、loginPassword三统一，且不同的终端硬件不能使用相同的终端信息</remarks>
        /// <returns></returns>
        private bool execLogin(string sessionId, Login request, out string terminalLocation)
        {
            terminalLocation = "";

            //检查登录信息是否已经被别的终端使用过
            lock (Sip2ServerProxy.Sessiones)
            {
                foreach (var kvp in Sip2ServerProxy.Sessiones)
                {
                    if (kvp.Value.LocationCode == request.LocationCode && kvp.Key != sessionId)
                    {
                        return false;
                    }
                }
            }

            XDocument xd = XDocument.Load("ScTerminalInfo.xml");

            var result = (from t in xd.Descendants("scTerminalInfo") where t.Element("locationCode").Value == request.LocationCode select t).FirstOrDefault();

            //检查终端信息是否存在
            if (result == null)
                return false;

            //检查locationCode、loginUserId、loginPassword是否三统一
            if (result.Element("locationCode").Value != request.LocationCode
                    || result.Element("loginUserId").Value != request.LoginUserId
                    || result.Element("loginPassword").Value != request.LoginPassword)
                return false;

            //读取终端的位置描述信息
            terminalLocation = result.Element("terminalLocation").Value;

            return true;
        }

        /// <summary>
        /// 执行还书操作
        /// </summary>
        /// <param name="request">还书消息</param>
        /// <param name="terminalLocationCode">自助设备的位置代码，不要超过10个西文字符或5个中文字符</param>
        /// <param name="execInfo">执行反馈信息</param>
        /// <param name="titleIdentifier">文献标题</param>
        /// <param name="permanentLocation">文献馆藏号</param>
        /// <param name="patronIdentifier">读者证件号</param>
        /// <returns></returns>
        private bool execCheckin(Checkin request, string terminalLocationCode, out string execInfo, out string titleIdentifier, out string permanentLocation, out string patronIdentifier)
        {
            execInfo = "";
            titleIdentifier = "";
            patronIdentifier = "";
            permanentLocation = "";

            milnetsDataContext dc = new milnetsDataContext();

            //检查图书条码的有效性
            var codebar = (from t in dc.l_codebars where t._图书条码 == request.ItemIdentifier select t).FirstOrDefault();

            if (codebar == null)
            {
                //条码不存在
                execInfo = string.Format(Error_ItemNotExist, request.ItemIdentifier);

                return false;
            }
            else
            {
                //借书记录
                var loan = (from t in dc.l_loans where t._图书条码 == request.ItemIdentifier select t).FirstOrDefault();

                //0不可借；1可阅览；2可借;6未典藏；5修补；3借出
                if (codebar._状态标识 != '3' || loan == null)
                {
                    execInfo = string.Format(Error_ItemStatusIsNotCharged, request.ItemIdentifier);

                    return false;
                }
                else
                {
                    //还书人条码
                    patronIdentifier = loan._读者条码;

                    //插入还书日志
                    dc.ExecuteCommand(string.Format("INSERT INTO l_loanlog(_图书条码,_读者条码,_日期,_操作说明,_操作员) VALUES('{0}','{1}','{2}','{3}','{4}')", request.ItemIdentifier, loan._读者条码, DateTime.Now.ToString(), "归还", terminalLocationCode));

                    //删除借还信息
                    int rtn = dc.ExecuteCommand(string.Format("DELETE FROM l_loan WHERE _图书条码='{0}'", loan._图书条码));

                    //更改条码状态标识
                    rtn = dc.ExecuteCommand(string.Format("UPDATE l_codebar SET _状态标识='2' WHERE _图书条码='{0}'", codebar._图书条码));

                    var bib = (from t in dc.bibs where t.recid == codebar.recid select t).FirstOrDefault();

                    if (bib != null)
                    {
                        titleIdentifier = bib._题名.Trim();
                        permanentLocation = string.Format("{0}/{1}", bib._分类.Trim(), bib._书次.Trim());
                    }

                    return true;
                }
            }

        }

        /// <summary>
        /// 执行借书操作
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        private void execCheckout(Checkout request, string locationCode, out CheckoutResponse response)
        {
            response = new CheckoutResponse();

            bool execSucess = false;

            string execInfo = "";
            string titleIdentifier = "";
            string dueDate = "";
            string feeAmount = "";

            milnetsDataContext dc = new milnetsDataContext();

            var reader = (from t in dc.l_readers where t._读者条码 == request.PatronIdentifier select t).FirstOrDefault();

            //读者条码无效
            if (reader == null)
            {
                execInfo = string.Format(Error_PatronNotExist, request.PatronIdentifier);
            }
            else
            {
                //检查读者是否可以借书
                char[] patronStatus = getPatronStatus(reader);

                if (patronStatus[0] == 'Y' || patronStatus[4] == 'Y' || patronStatus[5] == 'Y' || patronStatus[6] == 'Y' || patronStatus[10] == 'Y')
                {
                    if (patronStatus[0] == 'Y')
                    {
                        execInfo = Error_CheckoutDenied4Status;
                    }
                    else if (patronStatus[4] == 'Y')
                    {
                        execInfo = Error_CheckoutDenied4CardLost;
                    }
                    else if (patronStatus[5] == 'Y')
                    {
                        execInfo = Error_CheckoutDenied4ChargedLimit;
                    }
                    else if (patronStatus[6] == 'Y')
                    {
                        execInfo = Error_CheckoutDenied4OverdueLimit;
                    }
                    else if (patronStatus[10] == 'Y')
                    {
                        execInfo = Error_CheckoutDenied4FeeLimit;
                    }
                }
                else
                {
                    //检查图书是否可借
                    var codebar = (from t in dc.l_codebars where t._图书条码 == request.ItemIdentifier select t).FirstOrDefault();

                    if (codebar == null)
                    {
                        execInfo = string.Format(Error_ItemNotExist, request.ItemIdentifier);
                    }
                    else
                    {
                        if (codebar._状态标识 != '2')
                        {
                            execInfo = Error_ItemCheckoutDenied4Status;
                        }
                        else
                        {
                            var loans = from t in dc.l_loans where t._图书条码 == request.ItemIdentifier select t;

                            if (loans.Count() > 0)
                            {
                                execInfo = string.Format(Error_ItemHasCherged, request.ItemIdentifier, loans.First()._读者条码);
                            }
                            else
                            {
                                //得到图书分类
                                var bib = (from t in dc.bibs where t.recid == codebar.recid select t).FirstOrDefault();

                                if (bib == null)
                                {
                                    execInfo = Error_CheckoutDenied4BibinfoUnavailable;
                                }
                                else
                                {
                                    //得到当前图书的限类
                                    var abc = (from t in dc.l_para_abcs where t._分类 == bib._分类.Substring(0, 1) select t._限类).FirstOrDefault();

                                    if (abc == null)
                                    {
                                        execInfo = Error_CheckoutDenied4PolicyUnavailable;
                                    }
                                    else
                                    {
                                        //读者的借阅政策
                                        l_para para = (from t in dc.l_paras join r in dc.l_readers on t._读者类型 equals r._读者类型 where r._读者条码 == request.PatronIdentifier select t).First();

                                        if (para == null)
                                        {
                                            execInfo = Error_CheckoutDenied4PolicyUnavailable;
                                        }
                                        else
                                        {
                                            //当前限类的借阅政策
                                            int chargedDays;
                                            int chargeLimit;

                                            switch (abc)
                                            {
                                                case "甲":
                                                    chargedDays = para._甲限天数.Value;
                                                    chargeLimit = para._甲书册数.Value;
                                                    break;
                                                case "乙":
                                                    chargedDays = para._乙限天数.Value;
                                                    chargeLimit = para._乙书册数.Value;
                                                    break;
                                                case "丙":
                                                    chargedDays = para._丙限天数.Value;
                                                    chargeLimit = para._丙书册数.Value;
                                                    break;
                                                default:
                                                    chargedDays = para._甲限天数.Value;
                                                    chargeLimit = para._甲书册数.Value;
                                                    break;
                                            }

                                            //检查图书所在单类超出借阅上限没有
                                            var loanList = from t in dc.l_loans where t._读者条码 == request.PatronIdentifier select t;

                                            int chargedInAbc = 0;

                                            foreach (var item in loanList)
                                            {
                                                var k = (from p in dc.l_para_abcs join b in dc.bibs on p._分类 equals b._分类.Substring(0, 1) where b.recid == item.recid select p._限类).FirstOrDefault();

                                                if (k != null && k == abc)
                                                {
                                                    ++chargedInAbc;
                                                }
                                            }

                                            if (chargedInAbc + 1 > chargeLimit)
                                            {
                                                execInfo = string.Format(Error_CheckoutDenied4ClassChargedLimit, abc, chargeLimit);
                                            }
                                            else
                                            {
                                                //插入借阅信息
                                                dc.ExecuteCommand(string.Format("INSERT INTO l_loan VALUES('{0}','{1}','{2}','{3}','{4}','{5}',{6})", request.ItemIdentifier, request.PatronIdentifier, DateTime.Now, DateTime.Now.AddDays(chargedDays), "0", codebar.recid, 0));

                                                //插入借阅日志
                                                dc.ExecuteCommand(string.Format("INSERT INTO l_loanlog(_图书条码,_读者条码,_日期,_操作说明,_操作员) VALUES('{0}','{1}','{2}','{3}','{4}')", request.ItemIdentifier, request.PatronIdentifier, DateTime.Now.ToString(), "借阅", locationCode));

                                                //修改条码的状态标识
                                                dc.ExecuteCommand(string.Format("UPDATE l_codebar SET _状态标识='3' WHERE _图书条码='{0}'", request.ItemIdentifier));

                                                execSucess = true;

                                                titleIdentifier = bib._题名.Trim();
                                                dueDate = new string(GetDateCharArray(DateTime.Now.AddDays(chargedDays)));
                                                feeAmount = bib._价格.ToString();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            response.Ok = execSucess ? 'Y' : 'N';
            response.RenewalOk = 'N';
            response.MagneticMedia = Properties.Settings.Default.MagneticMedia;
            response.Desensitize = Properties.Settings.Default.Desensitize;
            response.TransactionDate = GetDateCharArray();
            response.InstitutionId = institutionId;
            response.PatronIdentifier = request.PatronIdentifier;
            response.ItemIdentifier = request.ItemIdentifier;
            response.TitleIdentifier = titleIdentifier;
            response.DueDate = dueDate;
            response.FeeType = Sip2Parameter.GetEnumCode(SipII.Parameter.enumFeeType.other_unknown).ToCharArray();
            response.FeeAmount = feeAmount;
            response.MediaType = Sip2Parameter.GetEnumCode(SipII.Parameter.enumMediaType.book).ToCharArray();
            response.ScreenMessage = execInfo;
        }

        /// <summary>
        /// 获取终端位置信息
        /// </summary>
        /// <param name="locationCode"></param>
        /// <returns></returns>
        private string getTerminalLocation(string locationCode)
        {

            XDocument xd = XDocument.Load("ScTerminalInfo.xml");

            var result = (from t in xd.Descendants("scTerminalInfo") where t.Element("locationCode").Value == locationCode select t).FirstOrDefault();

            if (result == null)
            {
                return "";
            }
            else
            {
                return result.Element("terminalLocation").Value;
            }
        }

        /// <summary>
        /// 更改文献属性
        /// </summary>
        /// <param name="request"></param>
        /// <param name="execInfo"></param>
        /// <returns></returns>
        private bool execItemStatusUpdate(ItemStatusUpdate request, out string execInfo)
        {
            execInfo = Error_SystemNotSupported;

            return false;
        }

        /// <summary>
        /// 续借图书
        /// </summary>
        /// <param name="PatronIdentifier">读者证件号</param>
        /// <param name="ItemIdentifier">图书条码号</param>
        /// <param name="itemTitle">图书标题</param>
        /// <param name="dueDate">超期时间</param>
        /// <param name="execInfo">执行反馈信息</param>
        /// <returns></returns>
        private bool execRenew(string PatronIdentifier, string ItemIdentifier, out string itemTitle, out string dueDate, out string execInfo)
        {

            milnetsDataContext dc = new milnetsDataContext();

            var loan = (from t in dc.l_loans where t._图书条码 == ItemIdentifier && t._读者条码 == PatronIdentifier select t).FirstOrDefault();

            bool RenewalOk = false;

            if (loan == null)
            {
                itemTitle = "";
                dueDate = new string(GetDateCharArray());
                execInfo = string.Format(Error_ItemNotCharged, PatronIdentifier, ItemIdentifier);
            }
            else
            {
                var bib = (from t in dc.bibs where t.recid == loan.recid select t._题名).FirstOrDefault();

                if (loan._续借标识 == '1')
                {
                    itemTitle = bib;
                    dueDate = new string(GetDateCharArray(loan._应还日期.Value));
                    execInfo = Error_ItemDuplicateRenewed;
                }
                else
                {
                    var reader = (from t in dc.l_readers where t._读者条码 == PatronIdentifier select t).FirstOrDefault();

                    char[] patronStatus = getPatronStatus(reader);

                    if (patronStatus[1] == 'Y')
                    {
                        itemTitle = bib;
                        dueDate = new string(GetDateCharArray(loan._应还日期.Value));
                        execInfo = Error_PatronRenewDenied;
                    }
                    else
                    {
                        var para = (from t in dc.l_paras where t._读者类型 == reader._读者类型 select t).FirstOrDefault();

                        if (para == null)
                        {
                            itemTitle = bib;
                            dueDate = new string(GetDateCharArray(loan._应还日期.Value));
                            execInfo = Error_RenewPolicyUnknow;
                        }
                        else
                        {
                            int renewDays = para._续借天数.Value;

                            dc.ExecuteCommand("UPDATE l_loan SET _续借标识='1',_应还日期=DATEADD(day,{0},_应还日期) WHERE _图书条码='{1}'", renewDays, ItemIdentifier);

                            itemTitle = bib;
                            dueDate = new string(GetDateCharArray(loan._应还日期.Value.AddDays(renewDays)));
                            execInfo = Error_PatronRenewDenied;

                            RenewalOk = true;
                        }
                    }
                }
            }

            return RenewalOk;
        }

        /// <summary>
        /// 执行预约
        /// </summary>
        /// <param name="request"></param>
        /// <param name="execInfo"></param>
        /// <returns></returns>
        private bool execHold(Hold request, out string execInfo)
        {
            execInfo = Error_AcsNotSupported;

            return false;
        }

        /// <summary>
        /// 执行缴费操作
        /// </summary>
        /// <param name="request"></param>
        /// <param name="execInfo"></param>
        /// <returns></returns>
        private bool execFeePaid(FeePaid request, out string execInfo)
        {
            execInfo = Error_SystemNotSupported;

            return false;
        }


        private char[] getPatronStatus(l_reader reader)
        {
            milnetsDataContext dc = new milnetsDataContext();

            int feeLimit = 0;
            int overdue = 0;

            int.TryParse(Properties.Settings.Default.Fee_Limit, out feeLimit);
            int.TryParse(Properties.Settings.Default.Overdue_Limit, out overdue);

            //借阅政策
            var loanPara = (from t in dc.l_paras where t._读者类型.Trim() == reader._读者类型.Trim() select t).FirstOrDefault();

            char[] patronStatus = new char[14];

            patronStatus[0] = reader._证件状态.Trim() == "正常" && loanPara._总限借数.Value > 0 ? ' ' : 'Y';                                               //拒绝借书
            patronStatus[1] = Properties.Settings.Default.Renew_Enable && reader._证件状态.Trim() == "正常" && loanPara._续借天数.Value > 0 ? ' ' : 'Y';   //拒绝续借
            patronStatus[2] = Properties.Settings.Default.Recall_Enable && reader._证件状态.Trim() == "正常" ? ' ' : 'Y';                                  //拒绝催书
            patronStatus[3] = Properties.Settings.Default.Hold_Enable && reader._证件状态.Trim() == "正常" && loanPara._限约册数 > 0 ? ' ' : 'Y';          //拒绝预约
            patronStatus[4] = reader._证件状态.Trim() == "挂失" ? 'Y' : ' ';                                                                               //卡已挂失

            var loanList = from t in dc.l_loans where t._读者条码 == reader._读者条码 select t;

            patronStatus[5] = loanList.Count() == loanPara._总限借数 ? 'Y' : ' ';                                   //在借图书过多
            patronStatus[6] = loanList.Where(t => t._应还日期.Value < DateTime.Now).Count() > overdue ? 'Y' : ' ';  //超期图书过多
            patronStatus[7] = ' ';                                                                                  //续借图书过多
            patronStatus[8] = ' ';                                                                                  //被索赔图书过多
            patronStatus[9] = ' ';                                                                                  //遗失图书过多
            if (reader._欠款金额 != null)
            {
                patronStatus[10] = reader._欠款金额.Value > feeLimit ? 'Y' : ' ';                                       //未付罚款过多
                patronStatus[11] = reader._欠款金额.Value > feeLimit ? 'Y' : ' ';                                       //拖欠费用过多
            }
            patronStatus[12] = ' ';                                                                                 //催书超期
            patronStatus[13] = ' ';                                                                                 //污损图书过多

            return patronStatus;
        }

        /// <summary>
        /// 计算各种类型的图书数量
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="statType"></param>
        /// <returns></returns>
        private char[] getItemsCount(l_reader reader, calcType calcType)
        {
            milnetsDataContext dc = new milnetsDataContext();

            int count = 0;

            if (calcType == calcType.hold)
            {
                var list = from t in dc.l_reserves where t._读者条码 == reader._读者条码 select t;

                count = list.Count();
            }

            if (calcType == calcType.overdue)
            {
                var list = from t in dc.l_loans where t._读者条码 == reader._读者条码 && t._应还日期.Value < DateTime.Now select t;

                count = list.Count();
            }

            if (calcType == calcType.charged)
            {
                var list = from t in dc.l_loans where t._读者条码 == reader._读者条码 select t;

                count = list.Count();
            }

            return count.ToString("D4").ToCharArray();
        }

        /// <summary>
        /// 计算各种操作的限制数
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="calcType"></param>
        /// <returns></returns>
        private char[] getLimitCout(l_reader reader, calcType calcType)
        {
            milnetsDataContext dc = new milnetsDataContext();

            var loanPara = (from t in dc.l_paras where t._读者类型.Trim() == reader._读者类型.Trim() select t).First();

            int count = 0;

            if (calcType == calcType.hold)
            {
                if (string.IsNullOrEmpty(Properties.Settings.Default.Hold_Limit.Trim()))
                {
                    count = loanPara._限约册数.Value;
                }
                else
                {
                    if (!int.TryParse(Properties.Settings.Default.Hold_Limit, out count))
                    {
                        count = 0;
                    }
                }
            }

            if (calcType == calcType.charged)
            {
                if (string.IsNullOrEmpty(Properties.Settings.Default.Charged_Limit.Trim()))
                {
                    count = loanPara._总限借数.Value;
                }
                else
                {
                    if (!int.TryParse(Properties.Settings.Default.Charged_Limit, out count))
                    {
                        count = 0;
                    }
                }
            }

            return count.ToString("D4").ToCharArray();
        }

        /// <summary>
        /// 获取图书队列
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private List<string> getItems(l_reader reader, calcType type)
        {
            milnetsDataContext dc = new milnetsDataContext();

            if (type == calcType.charged)
            {
                var result = from t in dc.l_loans where t._读者条码 == reader._读者条码 select t._图书条码;

                return result.ToList();
            }

            if (type == calcType.overdue)
            {
                var result = from t in dc.l_loans where t._读者条码 == reader._读者条码 && t._应还日期.Value < DateTime.Now select t._图书条码;

                return result.ToList();
            }

            if (type == calcType.hold)
            {
                var result = from t in dc.l_reserves where t._读者条码 == reader._读者条码 select t._题名;

                return result.ToList();
            }

            return new List<string>() { };
        }

        /// <summary>
        /// 获取流通状态
        /// </summary>
        /// <param name="statusCode"></param>
        /// <returns></returns>
        private char[] getCirculationStatus(char statusCode)
        {
            //0不可借；1可阅览；2可借;6未典藏；5修补；3借出

            enumCirculationstatus result;

            switch (statusCode)
            {
                case '0':
                    result = enumCirculationstatus.other;
                    break;
                case '1':
                    result = enumCirculationstatus.other;
                    break;
                case '2':
                    result = enumCirculationstatus.available;
                    break;
                case '3':
                    result = enumCirculationstatus.charged;
                    break;
                case '5':
                    result = enumCirculationstatus.in_process;
                    break;
                case '6':
                    result = enumCirculationstatus.in_process;
                    break;
                default:
                    result = enumCirculationstatus.other;
                    break;
            }

            return ((int)result).ToString("D2").ToCharArray();
        }
    }
}
